package faf.web.data;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class ResetFormValidator implements Validator {

	@Override
	public boolean supports(Class clazz) {
		return ResetForm.class.equals(clazz);
	}

	@Override
	public void validate(Object command, Errors errors) {
		if(command == null) {
			errors.rejectValue("email", "value-required");
			return;
		}
		
		ResetForm form = (ResetForm) command;
		
		Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
		Matcher m = p.matcher(form.getEmail());
		boolean matchFound = m.matches();
		
		if(!matchFound) {
			errors.rejectValue("email", "email-invalid");
			return;
		}		
		
	}

}
